#!/bin/bash
version=$(date -d "8 hour" -u +%y.%m.%d.%H)
cd ~/app-root/data/github-sh/xpi/abp
latest=$(curl -s https://addons.mozilla.org/zh-CN/firefox/addon/adblock-plus/versions/ |grep '\.xpi'|awk -F '\"' '{print $2}'|sed '/beta/d' |sed '2,$d')
if ! grep -q "$latest" ~/app-root/data/temp/xpi/abp/latest.txt; then
echo "$latest" > ~/app-root/data/temp/xpi/abp/latest.txt
wget ${latest} --output-document=abp-${version}.xpi
unzip -o abp-${version}.xpi bootstrap.js chrome/locale/en-US/* chrome/locale/zh-CN/* -d do/
zip -d abp-${version}.xpi META-INF/\* chrome/locale/\* chrome/content/ui/skin/\* chrome/content/ui/firstRun* 
mv -f abp-${version}.xpi do/abp-${version}.xpi
cd do/
sed -i '/prefs.json.*subscriptions_auto/c\require.scopes["prefs.json"] = {exports: {"defaults": {"subscriptions_autoupdate": true, "clearStatsOnHistoryPurge": true, "previewimages": true, "defaultstatusbaraction": 0, "notificationurl": "https://notification.adblockplus.org/notification.json", "notifications_showui": false, "report_submiturl": "https://reports.adblockplus.org/submitReport?version=1&guid=%GUID%&lang=%LANG%", "showinstatusbar": false, "patternsbackupinterval": 24, "sendReport_key": "", "enable_key": "", "currentVersion": "0.0", "detachsidebar": false, "data_directory": "adblockplus", "blockableItemsSize": {"width": 200, "height": 200}, "savestats": false, "sidebar_key": "Accel Shift V, Accel Shift U", "notifications_ignoredcategories": [], "please_kill_startup_performance": true, "filters_key": "Accel Shift E, Accel Shift F, Accel Shift O", "whitelistschemes": "about chrome file irc moz-extension moz-safe-about news resource snews x-jsd addbook cid imap mailbox nntp pop data javascript moz-icon", "frameobjects": true, "fastcollapse": false, "hideContributeButton": true, "flash_scrolltoitem": true, "recentReports": [], "suppress_first_run_page": true, "enabled": true, "notificationdata": {}, "subscriptions_listurl": "https://halflife.oschina.io/index/subscriptions2.xml", "patternsbackups": 0, "composer_default": 2, "documentation_link": "https://adblockplus.org/redirect?link=%LINK%&lang=%LANG%", "defaulttoolbaraction": 0}, "preconfigurable": ["suppress_first_run_page"]}};' bootstrap.js
sed -i "s#[0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{2\}#${version}#" install.rdf
zip -r abp-${version}.xpi *
rm -rf chrome/locale/ bootstrap.js
rm -f ~/app-root/runtime/repo/扩展/$(ls ~/app-root/runtime/repo/扩展/|grep "adblockplus2-[0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{2\}.xpi")
mv -f abp-${version}.xpi ~/app-root/repo/扩展/adblockplus2-${version}.xpi
~/app-root/data/github-sh/log/log.sh "AdblockPlus2-${version}.xpi 更新"
fi
